//
//  MessageController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/7/30.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "MessageController.h"
#import "SelectImageView.h"
#import <TZImagePickerController/TZImagePickerController.h>
@interface MessageController ()<TZImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet BoardTextField *nameLabel;
@property (weak, nonatomic) IBOutlet BoardTextField *danweiLabel;
@property (nonatomic ,strong) YMTextView *contentView;;

@property (weak, nonatomic) IBOutlet UIView *backView;

@property (weak, nonatomic) IBOutlet SelectImageView *selectImageView;
@property (nonatomic,strong)NSArray *photos;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *imageArray1;
@property (nonatomic, strong) NSMutableArray *nameArray;

@property (nonatomic, strong) NSMutableArray *infoArray;
@property (nonatomic, strong) NSMutableArray *contentArray;

@end

@implementation MessageController

-(NSMutableArray *)infoArray{
    if (!_infoArray) {
        _infoArray = [NSMutableArray array];
    }
    return _infoArray;
}

-(NSMutableArray *)contentArray{
    if (!_contentArray) {
        _contentArray = [NSMutableArray array];
    }
    return _contentArray;
}

-(NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}
-(NSMutableArray *)imageArray1{
    if (!_imageArray1) {
        _imageArray1 = [NSMutableArray array];
    }
    return _imageArray1;
}
-(NSMutableArray *)nameArray{
    if (!_nameArray) {
        _nameArray = [NSMutableArray array];
    }
    return _nameArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"投诉检举";
    self.contentView=[[YMTextView alloc]init];
    
    self.contentView.backgroundColor = [UIColor lightGrayColor];
    self.contentView.layer.masksToBounds =YES;
    self.contentView.layer.cornerRadius = 5;
    self.contentView.placeholder = @"";
   
   [self.backView addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}

#pragma mark - Table view data source

- (IBAction)imageSelectBtn:(UIButton *)sender {
    if(self.photos.count >=4){
              UIAlertView *aler  = [[UIAlertView alloc]initWithTitle:@"" message:@"最多可以选择4张哦" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
              [aler show];
          }
          TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:4 delegate:self];
          
          // 你可以通过block或者代理，来得到用户选择的照片.
       WS(weakSelf);
          [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets,BOOL isSelectOriginalPhoto) {
              weakSelf.selectImageView.images = photos;
              weakSelf.photos = photos;
              __block NSMutableArray *tempArray = photos;
              weakSelf.selectImageView.deleteItem = ^(NSInteger index) {
                  [tempArray removeObjectAtIndex:index];
                  weakSelf.selectImageView.images = tempArray;
                  weakSelf.photos = tempArray;
                  
                  
                 
              };
              [[ApiManager ShardInstance]upLoadImageimageArray:weakSelf.photos resultBlock:^(NSDictionary *data, NSError *error) {
                  
                  [weakSelf.imageArray addObjectsFromArray:data[@"rs"]];
                   for (int i = 0; i<weakSelf.imageArray.count; i++) {
                                       [weakSelf.imageArray1 addObject:[NSString stringWithFormat:@"android/%@",weakSelf.imageArray[i]]];
                                   }
                  
                  
              }];
          }];
       
        
       
          [self presentViewController:imagePickerVc animated:YES completion:nil];
    
}



- (IBAction)commitBtn:(BoardButton *)sender {
    
    for (int i =0; i< self.imageArray.count; i++) {
        
        [self.nameArray addObject:[NSString stringWithFormat:@"iosm%d",i]];
        
    }
    
//    [self.infoArray addObject:self.nameLabel.text];
//    [self.contentArray addObject:self.danweiLabel.text];
    
    
    WS(weakSelf);
    [[ApiManager ShardInstance]getInfoPoststaff_id:[TokenManager getUserId] type:@"2" content:self.contentView.textView.text name:self.nameLabel.text work_address:self.danweiLabel.text photo_name:self.nameArray photo_urls:self.imageArray1 resultBlock:^(NSDictionary *data, NSError *error) {
        
        
        BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
        if (model.code == 1) {
            [[ToastManager shardInstance]showToast:@"投诉成功"];
            
            NSInteger index=[[self.navigationController viewControllers]indexOfObject:self];
            [weakSelf.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index-2]animated:YES];
        }
        
    }];
    
}


@end
